package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SmsChannel;
import com.hzhc.entity.SmsLog;
import com.hzhc.entity.SmsTemplate;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.mapper.SmsChannelMapper;
import com.hzhc.mapper.SmsLogMapper;
import com.hzhc.mapper.SmsTemplateMapper;
import com.hzhc.service.SmsLogService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.SmsLogVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/6/17 11:05
 * @description SmsLogServiceImpl
 */
@Service
@RequiredArgsConstructor
public class SmsLogServiceImpl extends ServiceImpl<SmsLogMapper, SmsLog> implements SmsLogService {

    private final SmsChannelMapper channelMapper;
    private final SmsTemplateMapper templateMapper;

    @Override
    public List<SmsLogVo> getSmsLogList(Page<SmsLogVo> page, SmsLogVo vo) {
        IPage<SmsLogVo> iPage = baseMapper.getSmsLogList(page,vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> addSmsLog(SmsLogVo vo) {
        SmsChannel channel = channelMapper.selectById(vo.getSmsChannelId());
        SmsTemplate template = templateMapper.selectById(vo.getSmsMessagelId());
        List<String> mobileList = vo.getPersonPhone();
        String mobiles = String.join(",", mobileList);//122xx,133xx,144xx
        //发送短信
        //ResultUtil<String> resultUtil = SmsUtils.sendMsg(mobiles);
        SmsLog log = new SmsLog();
        log.setSignature(channel.getSignature());
        log.setService(channel.getService());
        log.setApiTemplateCode(template.getApiTemplateCode());
        log.setTemplateName(template.getTemplateName());
        log.setTemplateContent(template.getTemplateContent());
        log.setSmsSendMobile(mobiles);
        log.setSmsResponseStatus(ResultUtilCodeEnum.SUCCESS.getMessage());
        log.setSmsResponseMsg("发送成功");
        log.setCreateTime(LocalDateTime.now());
        log.setUserId(vo.getUserId());
        boolean b = save(log);
        if (b){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }


}
